
/****************************************************************
*  * *Process the common configure interface
*  * *
*  * *file name:logmgr.h
*  * *
*  * *Copyright (C)2012 Cisco webex.
*  * *All rights reserved
*  * *
*  * *Author:zhenye
*  * *Date  :2013-04-27
*  * *
*  * ****************************************************************/

#ifndef __LOG_MGR_H__
#define __LOG_MGR_H__

MCT_EXPORT VOID DumpMsgNew(BOOL_T bstdout, LONG nLevel, CHAR *pcFile, LONG lineNum, std::string lpszFormat, ...);
MCT_EXPORT CHAR * GetProgramPath(LONG level, CHAR *szPath, LONG nMaxLen);

#define DumpMsg0(nLevel, lpszFormat) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat)

#define DumpMsg1(nLevel, lpszFormat, v1) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1)

#define DumpMsg2(nLevel, lpszFormat, v1, v2) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2)

#define DumpMsg3(nLevel, lpszFormat, v1, v2, v3) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3)

#define DumpMsg4(nLevel, lpszFormat, v1, v2, v3, v4) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4)

#define DumpMsg5(nLevel, lpszFormat, v1, v2, v3, v4, v5) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4, v5)

#define DumpMsg6(nLevel, lpszFormat, v1, v2, v3, v4, v5, v6) \
		DumpMsgNew(BOOL_FALSE, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4, v5, v6)

#define DumpMsgWithScrFlag0(bStdout, nLevel, lpszFormat) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat)

#define DumpMsgWithScrFlag1(bStdout, nLevel, lpszFormat, v1) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1)

#define DumpMsgWithScrFlag2(bStdout, nLevel, lpszFormat, v1, v2) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2)

#define DumpMsgWithScrFlag3(bStdout, nLevel, lpszFormat, v1, v2, v3) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3)

#define DumpMsgWithScrFlag4(bStdout, nLevel, lpszFormat, v1, v2, v3, v4) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4)

#define DumpMsgWithScrFlag5(bStdout, nLevel, lpszFormat, v1, v2, v3, v4, v5) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4, v5)

#define DumpMsgWithScrFlag6(bStdout, nLevel, lpszFormat, v1, v2, v3, v4, v5, v6) \
		DumpMsgNew(bStdout, nLevel, __FILE__, __LINE__, lpszFormat, v1, v2, v3, v4, v5, v6)

class CTextStream
{
    public:
        typedef enum MctRdix
        {
            mct_dc       = 0,
            mct_hex     = 1
        } EMCTRDIX;

    public :
        CTextStream()
        {
            Reset();
        }

        ~CTextStream()
        {
        }

        void Reset()
        {
            m_bHex    = BOOL_FALSE;
            m_strBuff =  "";
        }

        CTextStream& operator << (CHAR charater)
        {
            return *this << ((LONG) charater);
        }

        CTextStream& operator << (UCHAR uChar)
        {
            return *this << ((ULONG) uChar);
        }

        CTextStream& operator << (SHORT sNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%hd", sNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%hx", sNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;
        }

        CTextStream& operator << (USHORT usNum)
        {
            return *this << ((ULONG) usNum);
        }

        CTextStream& operator << (int iNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%d", iNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%x", iNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;
        }

        CTextStream& operator << (unsigned int uiNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%u", uiNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%x", uiNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;
        }

        CTextStream& operator << (LONG lNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%ld", lNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%lx", lNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;

        }

        CTextStream& operator << (ULONG ulNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%lu", ulNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%lx", ulNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;
        }

        CTextStream& operator << (DLONG dlNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%lld", dlNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%llx", dlNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;

        }

        CTextStream& operator << (DULONG dulNum)
        {
            CHAR acBuf[128];

            if(BOOL_TRUE != GetHexFlag())
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "%llu", dulNum);
            }
            else
            {
                MCT_snprintf(acBuf, sizeof(acBuf), "0X%llx", dulNum);
            }

            Advance(acBuf);
            SetHexFlag(false);

            return *this;
        }

        CTextStream& operator << (FLOAT fNum)
        {
            CHAR acBuf[128];

            MCT_snprintf(acBuf, sizeof(acBuf), "%f", fNum);

            Advance(acBuf);

            return *this;
        }

        CTextStream& operator << (double dNum)
        {
            CHAR acBuf[128];

            MCT_snprintf(acBuf, sizeof(acBuf), "%f", dNum);

            Advance(acBuf);

            return *this;
        }

        CTextStream& operator << (std::string strString)
        {
            Advance(strString);
            return *this;
        }

        CTextStream& operator << (CHAR* szString)
        {
            Advance(szString);
            return *this;
        }

        CTextStream& operator << (VOID *ptrVoid)
        {
            CHAR acBuf[128];

            MCT_snprintf(acBuf, sizeof(acBuf), "%p", ptrVoid);

            Advance(acBuf);
            return *this;
        }

        CTextStream& operator << (EMCTRDIX ordix)
        {
            switch(ordix)
            {
                case mct_hex:
                {
                    SetHexFlag(BOOL_TRUE);
                    break;
                }
                default:
                {
                    SetHexFlag(BOOL_FALSE);
                    break;
                }
            }
            return *this;
        }

        std::string getString () const
        {
            return m_strBuff;
        }

    private :
        void SetHexFlag(BOOL_T bValue)
        {
            m_bHex = bValue;
        }

        BOOL_T GetHexFlag()
        {
            return m_bHex;
        }

        void Advance(std::string strString)
        {
            m_strBuff += strString;
        }

        void Advance(CHAR *szString)
        {
            m_strBuff += szString;
        }

private :
    std::string m_strBuff;
    BOOL_T m_bHex;
};

#define MCT_STRACE(level, outStr)   \
{  \
    CTextStream oText; \
    oText<<outStr; \
    DumpMsg0(level, (oText.getString()));\
}


#endif // __LOG_MGR_H__
